package br.com.it4.zmed.view.backbean;

import java.util.Calendar;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.event.AjaxBehaviorEvent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import br.com.it4.framework.util.exception.SystemException;
import br.com.it4.zmed.entity.Atendimento;
import br.com.it4.zmed.entity.Convenio;
import br.com.it4.zmed.entity.Exame;
import br.com.it4.zmed.entity.Medico;
import br.com.it4.zmed.entity.Paciente;
import br.com.it4.zmed.filter.MedicoFilter;
import br.com.it4.zmed.filter.PacienteFilter;
import br.com.it4.zmed.service.AtendimentoService;
import br.com.it4.zmed.service.ConvenioService;
import br.com.it4.zmed.service.ExameService;
import br.com.it4.zmed.service.MedicoService;
import br.com.it4.zmed.service.PacienteService;

/**
 * Backing bean para o laudo do paciente.
 */
@Component("laudoBean")
@Scope("session")
public class LaudoBean extends ZMedBean {

    @Autowired
    private PacienteService pacienteService;

    @Autowired
    private MedicoService medicoService;

    @Autowired
    private ConvenioService convenioService;

    @Autowired
    private ExameService exameService;

    @Autowired
    private AtendimentoService atendimentoService;

    private Atendimento atendimento;

    private String nomeFiltro;

    private List<Paciente> pacientes;

    private List<Medico> medicos;

    private List<Convenio> convenios;

    private List<Exame> exames;

    /**
     * @return the atendimento
     */
    public Atendimento getAtendimento() {
        return atendimento;
    }

    /**
     * @param atendimento
     *            the atendimento to set
     */
    public void setAtendimento(Atendimento atendimento) {
        this.atendimento = atendimento;
    }

    /**
     * @return the pacientes
     */
    public List<Paciente> getPacientes() {
        return pacientes;
    }

    /**
     * @return the medicos
     */
    public List<Medico> getMedicos() {
        return medicos;
    }

    /**
     * @return the nomeFiltro
     */
    public String getNomeFiltro() {
        return nomeFiltro;
    }

    /**
     * @param nomeFiltro
     *            the nomeFiltro to set
     */
    public void setNomeFiltro(String nomeFiltro) {
        this.nomeFiltro = nomeFiltro;
    }

    /**
     * @param pacientes
     *            the pacientes to set
     */
    public void setPacientes(List<Paciente> pacientes) {
        this.pacientes = pacientes;
    }

    /**
     * @return the convenios
     */
    public List<Convenio> getConvenios() {
        return convenios;
    }

    /**
     * @param convenios
     *            the convenios to set
     */
    public void setConvenios(List<Convenio> convenios) {
        this.convenios = convenios;
    }

    /**
     * @return the exames
     */
    public List<Exame> getExames() {
        return exames;
    }

    /**
     * @param exames
     *            the exames to set
     */
    public void setExames(List<Exame> exames) {
        this.exames = exames;
    }

    @SuppressWarnings("unused")
    @PostConstruct
    private void init() throws SystemException {
        this.convenios = this.convenioService.getConvenios();
        this.exames = this.exameService.getExames();

        this.atendimento = new Atendimento();
        this.atendimento.setData(Calendar.getInstance().getTime());
    }

    public void showPopupPaciente(AjaxBehaviorEvent abe) throws SystemException {
        this.nomeFiltro = "";
        this.pacientes = this.pacienteService.getPacientes(new PacienteFilter(), 0, getPageSize());
    }

    public void searchPaciente(AjaxBehaviorEvent abe) throws SystemException {
        synchronized (this.pacientes) {
            PacienteFilter filter = new PacienteFilter();
            filter.setNome(this.nomeFiltro);
            this.pacientes = this.pacienteService.getPacientes(filter, 0, getPageSize());
        }
    }

    public void showPopupMedico(AjaxBehaviorEvent abe) throws SystemException {
        this.nomeFiltro = "";
        this.medicos = this.medicoService.getMedicos(new MedicoFilter(), 0, getPageSize());
    }

    public void searchMedico(AjaxBehaviorEvent abe) throws SystemException {
        synchronized (this.medicos) {
            MedicoFilter filter = new MedicoFilter();
            filter.setNome(this.nomeFiltro);
            this.medicos = this.medicoService.getMedicos(filter, 0, getPageSize());
        }
    }

}
